package spring.test1;

import com.blackhorse.dao.AccountDao;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Demo1_CreateBean {
    private ApplicationContext applicationContext;

    @Before
    public void setUp() {
        //1.获取spring核心容器
        applicationContext = new ClassPathXmlApplicationContext("ioc/applicationContext.xml");
    }

    @Test
    public void testDefaultCreate() {
        System.out.println("-------------默认构造创建------------");
        AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao");
        System.out.println(accountDao);
        accountDao.saveAccount();
    }

    @Test
    public void testFactoryCreate() {
        System.out.println("-------------实例工厂创建------------");
        AccountDao accountDaoByFactory = applicationContext.getBean("accountDaoByFactory", AccountDao.class);
        System.out.println(accountDaoByFactory);
        accountDaoByFactory.saveAccount();
    }

    @Test
    public void testStaticFactoryCreate(){
        System.out.println("-------------静态工厂创建------------");
        AccountDao accountDaoByStaticFactory = applicationContext.getBean("accountDaoByStaticFactory", AccountDao.class);
        System.out.println(accountDaoByStaticFactory);
        accountDaoByStaticFactory.saveAccount();
    }
}
